<template>
  <div>
    <!-- 搜索页面头部 -->
    <div class="search-header">
      <!-- 后退按钮 -->
      <van-icon
        name="arrow-left"
        color="white"
        size="0.48rem"
        class="goback"
        @click="$router.back()"
      />
      <!-- 搜索组件 -->
      <van-search
        ref="A"
        placeholder="请输入搜索关键词"
        background="#007BFF"
        shape="round"
        v-gfocus
        @input="input"
        v-model.trim="kw"
        @keydown.enter="goResult"
      />
    </div>
    <!-- 搜索历史 -->
    <div class="search-history" v-if="!kw">
      <!-- 标题 -->
      <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-icon name="delete" class="search-icon" @click="qingKong" />
        </template>
      </van-cell>

      <!-- 历史列表 -->
      <div class="history-list">
        <span
          class="history-item"
          v-for="(item, index) in history"
          :key="index"
          @click="goResultHistory(item)"
          @contextmenu.prevent
          @selectstart.prevent
          @touchstart="touchstart(index)"
          @touchend="touchend"
          >{{ item }}</span
        >
      </div>
    </div>
    <!-- 联想菜单展示 -->
    <div
      @click="goResultStr(item)"
      v-else
      v-for="(item, index) in menu"
      :key="index"
    >
      <LianxiangMenu :menu="item" :kw="kw" />
    </div>
  </div>
</template>

<script>
import { Dialog } from 'vant'
import LianxiangMenu from './component/LianxiangMenu.vue'
import { LxMenu } from '@/api/home.js'
export default {
  components: {
    LianxiangMenu
  },
  data () {
    return {
      kw: '', // 搜索关键字
      time: 0, // 演示器id
      menu: [],
      history: JSON.parse(localStorage.getItem('history')) || [],
      id: 0
    }
  },
  methods: {
    // 长按删除单项
    touchstart (a) {
      this.id = setTimeout(() => {
        Dialog.confirm({
          message: '确定要删除当前选项吗?'
        })
          .then(() => {
            console.log(a)
            this.history.splice(a, 1)
          })
          .catch(() => {
            // on cancel
          })
      }, 1000)
    },
    touchend () {
      clearTimeout(this.id)
    },
    //   输入框防抖
    input () {
      clearTimeout(this.time)
      this.time = setTimeout(async () => {
        if (this.kw) {
          const resp = await LxMenu(this.kw)
          this.menu = resp.data.options
        } else {
          this.menu = []
        }
      }, 500)
    },
    // 跳转页面
    goResult () {
      this.history.push(this.kw)
      this.history = Array.from(new Set(this.history)) // 去重
      setTimeout(() => {
        this.$router.push({
          path: '/search/' + this.kw
        })
      }, 20)
    },
    goResultHistory (a) {
      this.history.push(a)
      this.history = Array.from(new Set(this.history)) // 去重
      setTimeout(() => {
        this.$router.push({
          path: '/search/' + a
        })
      }, 20)
    },
    goResultStr (item) {
      this.history.push(item)
      this.history = Array.from(new Set(this.history)) // 去重
      setTimeout(() => {
        this.$router.push({
          path: '/search/' + item
        })
      }, 20)
    },
    qingKong () {
      this.history = []
      localStorage.setItem('history', [])
    }
  },
  watch: {
    history () {
      localStorage.setItem('history', JSON.stringify(this.history))
    }
  },
  activated () {
    this.$refs.A.querySelector('input').focus()
  },
  deactivated () {
    this.kw = ''
  }
}
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }
}
.search-icon {
  font-size: 16px;
  line-height: inherit;
}

.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>
